package com.alone.jpa.entity.enhancer;

import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.util.List;

/**
 * 通过 @EntityListeners(GlobalEntityListener.class) 开启功能
 *
 * @author Alone
 */
@SuppressWarnings("all")
@Slf4j
public class GlobalEntityListener {

    private final List<EntityEnhanceHandler> handlers;

    public GlobalEntityListener(List<EntityEnhanceHandler> handlers) {
        log.info("has load handler: {}", handlers);
        this.handlers = handlers;
    }

    @PrePersist
    public void prePersist(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.prePersist(object);
        }
    }

    @PreUpdate
    public void preUpdate(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.preUpdate(object);
        }
    }

    @PostPersist
    public void postPersist(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.postPersist(object);
        }
    }

    @PostUpdate
    public void postUpdate(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.postUpdate(object);
        }
    }

    @PostLoad
    public void postLoad(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.postLoad(object);
        }
    }

    @PreRemove
    public void preRemove(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.preRemove(object);
        }
    }

    @PostRemove
    public void postRemove(Object object) {
        for (EntityEnhanceHandler handler : handlers) {
            handler.postRemove(object);
        }
    }
}
